﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_BGM_MAS_ExpenseGroup : IDAL_BGM_MAS_ExpenseGroup
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_BGM_MAS_ExpenseGroup> GetFull(Nullable<int> bgm_source_seq, bool bLeafOnly)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_ExpenseGroup> infoList = new List<M_BGM_MAS_ExpenseGroup>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT expg.bgm_exp_grp_seq, expg.bgm_exp_grp_code, expg.bgm_exp_grp_name"
                    + ", expg.bgm_exp_grp_descr, expg.upper_bgm_exp_grp_seq"
                    + ", expg.bgm_source_seq, s.bgm_source_name"
                    + ", expg.record_status"
                    + " FROM bgm_mas_expense_group expg"
                    + " LEFT JOIN bgm_mas_source s ON expg.bgm_source_seq = s.bgm_source_seq"
                    + " LEFT JOIN bgm_mas_expense_group lexpg ON expg.bgm_exp_grp_seq = lexpg.upper_bgm_exp_grp_seq"
                        + " AND lexpg.record_status not like @delete_status"
                    + " WHERE expg.record_status not like @delete_status"
                    + (bgm_source_seq != null ? " and expg.bgm_source_seq = @bgm_source_seq" : "")
                    + (bLeafOnly ? " and lexpg.bgm_exp_grp_seq is null" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_ExpenseGroup info = new M_BGM_MAS_ExpenseGroup();
                        info.bgm_exp_grp_seq = Convert.ToInt32(drc[i]["bgm_exp_grp_seq"]);
                        info.bgm_exp_grp_code = drc[i]["bgm_exp_grp_code"].ToString();
                        info.bgm_exp_grp_name = drc[i]["bgm_exp_grp_name"].ToString();
                        info.bgm_exp_grp_descr = drc[i]["bgm_exp_grp_descr"].ToString();
                        info.upper_bgm_exp_grp_seq = cn.ConvertNullInt(drc[i]["upper_bgm_exp_grp_seq"]);
                        info.bgm_source_seq = cn.ConvertNullInt(drc[i]["bgm_source_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_ExpenseGroup> GetList(Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_ExpenseGroup> infoList = new List<M_BGM_MAS_ExpenseGroup>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT expg.bgm_exp_grp_seq, expg.bgm_exp_grp_name"
                    + ", expg.record_status"
                    + " FROM bgm_mas_expense_group expg"
                    //+ " LEFT JOIN bgm_mas_expense_group lexpg ON expg.bgm_exp_grp_seq = lexpg.upper_bgm_exp_grp_seq"
                    //    + " AND lexpg.record_status not like @delete_status"
                    + " WHERE expg.record_status not like @delete_status"
                    + (bgm_source_seq != null ? " and expg.bgm_source_seq = @bgm_source_seq" : "");
                    //+ " and lexpg.bgm_exp_grp_seq is null";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_ExpenseGroup info = new M_BGM_MAS_ExpenseGroup();
                        info.bgm_exp_grp_seq = Convert.ToInt32(drc[i]["bgm_exp_grp_seq"]);
                        info.bgm_exp_grp_name = drc[i]["bgm_exp_grp_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_BGM_MAS_ExpenseGroup GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_MAS_ExpenseGroup info = new M_BGM_MAS_ExpenseGroup();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@seq", seq);

                strSQL = "SELECT expg.bgm_exp_grp_seq, expg.bgm_exp_grp_code, expg.bgm_exp_grp_name"
                    + ", expg.bgm_exp_grp_descr"//, expg.upper_bgm_exp_grp_seq"
                    + ", expg.bgm_source_seq, s.bgm_source_name"
                    + ", expg.record_status"
                    + " FROM bgm_mas_expense_group expg"
                    + " LEFT JOIN bgm_mas_source s ON expg.bgm_source_seq = s.bgm_source_seq"
                    //+ " LEFT JOIN bgm_mas_expense_group lexpg ON expg.bgm_exp_grp_seq = lexpg.upper_bgm_exp_grp_seq"
                    //    + " AND lexpg.record_status not like @delete_status"
                    + " WHERE expg.bgm_exp_grp_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_exp_grp_seq = Convert.ToInt32(drc[0]["bgm_exp_grp_seq"]);
                    info.bgm_exp_grp_code = drc[0]["bgm_exp_grp_code"].ToString();
                    info.bgm_exp_grp_name = drc[0]["bgm_exp_grp_name"].ToString();
                    info.bgm_exp_grp_descr = drc[0]["bgm_exp_grp_descr"].ToString();
                    //info.upper_bgm_exp_grp_seq = cn.ConvertNullInt(drc[0]["upper_bgm_exp_grp_seq"]);
                    info.bgm_source_seq = cn.ConvertNullInt(drc[0]["bgm_source_seq"]);
                    info.bgm_source_name = drc[0]["bgm_source_name"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
